home *** CD-ROM | disk | FTP | other *** search
/ bioinformatics.org / bioinformatics.org_software.tar / www.bioinformatics.org / download / ecell2 / ecell220setup.exe / {app} / standard / SRCJ / EntrySelector.java < prev    next >
Text File  |  2002-07-10  |  18KB  |  622 lines

  1.  
  2. /**
  3.  * title:     EntrySelector class (EntrySelector.java)<p>
  4.  * illustration  : EntrySelector window class<p>
  5.  * Copyright (C) 1996-2001 Keio University <p>
  6.  * Copyright (C) 1998-2001 Japan Science and Technology Corporation (JST)<p>
  7.  *               GNU General Public Licence <p>
  8.  * Division:     Mitsui Knowledge Industry Co. Ltd. Bioscience division <p>
  9.  * Version :     $Id: EntrySelector.java,v 1.7 2002/05/13 00:23:04 ota Exp $ <p>
  10.  */
  11.  
  12.  
  13. package ecell;
  14.  
  15. import javax.swing.*;
  16. import javax.swing.tree.*;
  17. import java.awt.BorderLayout;
  18. import java.awt.event.*;
  19. import java.awt.*;
  20. import javax.swing.plaf.basic.*;
  21. import javax.swing.border.*;
  22. import javax.swing.event.*;
  23. import java.io.*;
  24. import java.util.StringTokenizer;
  25. import java.util.Vector;
  26. import java.util.Hashtable;
  27.  
  28.  
  29. /**
  30.  * EntrySelector window class 
  31.  * @author         XD.Zheng
  32.  * @version  1.0
  33.  * @since  JDK1.2.2
  34.  */
  35.  
  36. class EntrySelector extends JFrame implements ActionListener
  37. {
  38.  
  39.  
  40.     JPanel  rightPanel = new JPanel(new BorderLayout());
  41.     JPanel  jp1 = new JPanel();
  42.     JPanel  jp2 = new JPanel();
  43.     JPanel  jp3 = new JPanel();
  44.     JButton buttonOk = new JButton("Ok");
  45.     JButton buttonCancel = new JButton("Cancel");
  46.     JRadioButton radioSubstance = new JRadioButton("Substance");
  47.     JRadioButton radioReactor = new JRadioButton("Reactor");
  48.     Border border1;
  49.     JLabel label1= new JLabel("System:");
  50.  
  51.  
  52.     /**
  53.      * Number indicating the called item  
  54.      */
  55.     int parent = 0;
  56.  
  57.     /**
  58.      * Selected item
  59.      */
  60.     Object object [] ;
  61.  
  62.     /**
  63.      * Tree structure
  64.      */
  65.     JTree   jTree     = new JTree();
  66.  
  67.     /**
  68.      * ListModel
  69.      */
  70.     DefaultListModel model = new DefaultListModel();
  71.  
  72.     /**
  73.      * List
  74.      */
  75.     JList jList;
  76.  
  77.     /**
  78.      * Full Path
  79.      */
  80.     String path = "";
  81.  
  82.     /**
  83.      * TreeModel
  84.      */
  85.     MyTreeModel treeModel; 
  86.  
  87.     /**
  88.      * Search for ListModel with System item as a key.
  89.      */
  90.     Hashtable listmode = new Hashtable();
  91.  
  92.     /**
  93.      * Current  TreeNode
  94.      */
  95.     String currentStr = null;
  96.  
  97.     /**
  98.      * Storing array of window ID 
  99.      */
  100.     int[] winId =new int[1];
  101.  
  102.     /**
  103.      * ClassName
  104.      */
  105.     String[] className;
  106.  
  107.     /**
  108.      * Selected number of ListItem
  109.      */
  110.     int idObject [];
  111.  
  112.     /**
  113.      * Parent tracer, required for being called from Tracer 
  114.      */
  115.     Tracer tracer;
  116.  
  117.     /**
  118.      * Number of radioButton  1 Substance 2 Reactor
  119.      */
  120.     int radioCode = 1;
  121.  
  122.     /**
  123.      * Instance of control panel
  124.      */
  125.     MainWindow window ;
  126.  
  127.  
  128.     /**
  129.      * Constructor
  130.      * @param parent   Target called by the program 0 Tracer 1 SubstanceItem 2 ReactorItem
  131.      * @param window    Controller Panel
  132.      */
  133.     public EntrySelector(int parent,MainWindow window){
  134.         this.parent = parent;
  135.         this.window = window;
  136.         if(parent==0){
  137.             radioSubstance.setSelected(true);
  138.             radioCode = 1;
  139.         }else if(parent == 1){
  140.             radioSubstance.setSelected(true);
  141.             radioReactor.setEnabled(false);
  142.             radioCode = 1;
  143.         }else if(parent == 2){    
  144.             radioReactor.setSelected(true);
  145.             radioSubstance.setEnabled(false);
  146.             radioCode = 2;
  147.         }
  148.  
  149.         try {
  150.             jbInit();
  151.  
  152.         }
  153.         catch(Exception e) {
  154.             window.messageWindow.setErrorMessage( e );
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * Set the window property according to the called item. 
  160.      * @param parent    Target called by the program 0 Tracer  1 SubstanceItem  2 ReactorItem
  161.      * @return void
  162.      */
  163.  
  164.     public void setEntrySelector(int parent){
  165.         this.parent = parent;
  166.         if(parent==0){
  167.             radioSubstance.setEnabled(true);
  168.             radioSubstance.setEnabled(true);
  169.             radioSubstance.setSelected(true);
  170.             radioCode = 1;
  171.         }else if(parent == 1){
  172.             radioSubstance.setEnabled(true);
  173.             radioSubstance.setSelected(true);
  174.             radioReactor.setEnabled(false);
  175.             radioCode = 1;
  176.         }else if(parent == 2){    
  177.             radioReactor.setEnabled(true);
  178.             radioReactor.setSelected(true);
  179.             radioSubstance.setEnabled(false);
  180.             radioCode = 2;
  181.         }
  182.  
  183.         if(radioSubstance.isSelected() ) {
  184.             radioCode =1;
  185.         } else if(radioReactor.isSelected() ) {
  186.             radioCode =2;
  187.         }
  188.         String str="";
  189.         if(currentStr != null){
  190.             if(radioCode ==1){
  191.                 str = currentStr +"65536";
  192.             }else{
  193.                 str =currentStr +"65537";
  194.             }
  195.         }
  196.         model.removeAllElements();
  197.         if(currentStr!=null)
  198.         setListMode(str);
  199.  
  200.     }
  201.  
  202.  
  203.     /**
  204.      * Initialize
  205.      * @return void
  206.      */
  207.     public void jbInit() /*throws Exception*/  {
  208.         this.setTitle("Entry Selector");
  209.         this.setSize(512,512);
  210.         treeModel= new  MyTreeModel(this);
  211.         jTree.addTreeSelectionListener( treeSListener );
  212.  
  213.  
  214.         border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(134, 134, 134),new Color(93, 93, 93));
  215.         String s = null;
  216.         try
  217.         {
  218.             s = window.df.getEntryData();
  219.         }
  220.         catch( Exception e )
  221.         {
  222.             window.messageWindow.setErrorMessage( e );
  223.         }
  224.  
  225.         jTree.setModel(treeModel.getMyTreeModel2(s) );
  226.         JScrollPane jScrollPane = new JScrollPane(jTree);
  227.  
  228.  
  229.         ButtonGroup buttonGroup = new ButtonGroup();
  230.         jList = new JList(model);
  231.         jList.setFont(new java.awt.Font("DialogInput", 0, 12));
  232.         jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  233.         jList.addListSelectionListener(listListener);
  234.  
  235.         jp2.setLayout(new FlowLayout(0,0,2));
  236.         jp2.add(label1);
  237.         jList.setBackground(Color.lightGray);
  238.         jList.setBorder(border1);
  239.         
  240.         buttonGroup.add(radioSubstance);
  241.         buttonGroup.add(radioReactor);
  242.         radioSubstance.addActionListener(actionListener);
  243.         radioReactor.addActionListener(actionListener);
  244.         jp1.add(radioSubstance);
  245.         jp1.add(radioReactor);
  246.  
  247.         rightPanel.add(jp1,"North");
  248.         rightPanel.add(new JScrollPane( jList ),"Center");
  249.  
  250.         JSplitPane jSplitPane =
  251.             new JSplitPane(
  252.             JSplitPane.HORIZONTAL_SPLIT,
  253.             jScrollPane,
  254.             rightPanel
  255.         );
  256.         // 2002.6.27
  257.         jSplitPane.setDividerLocation( 138 );
  258.         
  259.         jp3.setLayout(new FlowLayout(0,2,2));
  260.         jp3.add(buttonOk);
  261.         jp3.add(buttonCancel);
  262.         buttonOk.addActionListener(this);
  263.         buttonCancel.addActionListener(this);
  264.         this.getContentPane().add( jp2,BorderLayout.NORTH);
  265.         this.getContentPane().add( jSplitPane , BorderLayout.CENTER);
  266.         this.getContentPane().add( jp3 , BorderLayout.SOUTH);
  267.  
  268.     }
  269.  
  270.     /**
  271.      * The generation of ListMode
  272.      * @param  string  TreeNode character string
  273.      * @return void
  274.      */
  275.     void setListMode(String strKey){
  276.         Vector vr = (Vector)listmode.get(strKey);
  277.         if(vr !=null){
  278.             int num= vr.size();
  279.             winId = new int[num];
  280.             className= new String[num];
  281.             for(int i=0;i<num;i++){
  282.                 String str = (String)vr.elementAt(i);
  283.                 StringTokenizer st = new StringTokenizer(str,"\t");
  284.                 winId[i] = Integer.parseInt(st.nextToken());
  285.                 model.addElement(st.nextToken());
  286.                 className[i]=st.nextToken();
  287.                 
  288.             }
  289.         }
  290.  
  291.     }
  292.  
  293.     /**
  294.      * Set Field of Tracer
  295.      * @param tracer    Instance of Tracer
  296.      * @return void
  297.      */
  298.     public void setTracer(Tracer tracer){
  299.         this.tracer=tracer;
  300.     }
  301.  
  302.     /**
  303.      * Override to exit when the window is closed.
  304.      * @param e      WindowEvent
  305.      * @return void
  306.      */
  307.     protected void processWindowEvent(WindowEvent e) {
  308.         super.processWindowEvent(e);
  309.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  310.             this.setVisible(false);
  311.             
  312.         }
  313.     }
  314.  
  315.     /**
  316.      *TreeSelectionListener
  317.      */
  318.     private TreeSelectionListener treeSListener = new TreeSelectionListener() {
  319.         public void valueChanged(TreeSelectionEvent e) {
  320.             TreePath treePath = (TreePath ) e.getPath();
  321.             String abc = "";
  322.             int count  = treePath.getPathCount();
  323.             for(int i=0; i<count;i++){
  324.                 if (i>1) {
  325.                     abc += "/"+treePath.getPathComponent(i).toString();
  326.                 }else {
  327.                     abc += treePath.getPathComponent(i).toString();
  328.                 }
  329.             }
  330.             
  331.             path = abc;
  332.             DefaultMutableTreeNode symbol =
  333.                 (DefaultMutableTreeNode) treePath.getLastPathComponent();
  334.             
  335.             model.removeAllElements();
  336.             String str=(String) symbol.getUserObject();
  337.             currentStr =str;
  338.             if(radioCode==1){
  339.                 str=str+"65536";
  340.             }else{
  341.                 str=str+"65537";
  342.             }
  343.  
  344.             setListMode(str);
  345.  
  346.  
  347.         }
  348.     };
  349.  
  350.     /**
  351.      *Inter Class
  352.      */
  353.     private ActionListener actionListener = new ActionListener() {
  354.         public void actionPerformed(ActionEvent e) {
  355.             if(radioSubstance.isSelected() ) {
  356.                 radioCode =1;
  357.             } else if(radioReactor.isSelected() ) {
  358.                 radioCode =2;
  359.             }
  360.             String str="";
  361.             if(currentStr != null){
  362.                 if(radioCode ==1){
  363.                     str = currentStr +"65536";
  364.                 }else{
  365.                     str =currentStr +"65537";
  366.                 }
  367.             }
  368.             model.removeAllElements();
  369.             if(currentStr!=null)
  370.             setListMode(str);
  371.  
  372.         }
  373.     };
  374.  
  375.     /**
  376.      * ListSelectionListener
  377.      */
  378.     private ListSelectionListener listListener = new ListSelectionListener(){
  379.         public void valueChanged(ListSelectionEvent evt){
  380.             object  = (Object []) jList.getSelectedValues();
  381.             idObject =jList.getSelectedIndices();
  382.         }
  383.     };
  384.  
  385.     /**
  386.      * ActionPerformed
  387.      * @param e         ActionEvent  Event
  388.      * @return void
  389.      */
  390.     public void actionPerformed(ActionEvent e) {
  391.         
  392.         if(e.getSource()== buttonOk){
  393.  
  394.             if(object!=null){
  395.                 if(parent == 0){          
  396.                     if(tracer != null){
  397.                         int length = object.length;
  398.                         String[] tracers = new String[length];
  399.                         String[] kits = new String[length];
  400.                         int[] nums = new int[length];
  401.                         String[] cname = new String[length];
  402.  
  403.                         
  404.                         int[] id =new int[length+1];
  405.                         int[] wType=new int[length];
  406.                         for(int i = 0; i < length  ; i++) {
  407.                             String str0 = (String) object[i];
  408.                             
  409.                             int ind = str0.indexOf("]");
  410.                             String str1 = str0.substring(1,ind).trim();
  411.  
  412.                             nums[i]=winId[idObject[i]];
  413.                             cname[i]=className[idObject[i]];
  414.                             id[i]=winId[idObject[i]];
  415.                             String str2 = str0.substring(ind +1).trim();
  416.                             tracers[i]=str1;
  417.                             kits[i]= str2;
  418.                             wType[i]=radioCode;
  419.                             try{
  420.                                 window.df.setEntrySelected(tracer.tracerNumber,radioCode,id[i]);
  421.                             }catch(Exception ex){
  422.                                 window.messageWindow.setErrorMessage( ex );
  423.                             }
  424.                         }
  425.                         tracer.setCheckBox(tracers,kits,cname,nums,wType);
  426.                     }
  427.  
  428.                 }else if(parent == 1){
  429.                     int length = object.length;
  430.  
  431.                     for(int i = 0; i < object.length  ; i++) {
  432.                         String str0 = (String) object[i];
  433.                         //System.out.println(str0);
  434.                         int ind = str0.indexOf("]");
  435.                         String str1 = str0.substring(1,ind).trim();
  436.  
  437.                         int subCode =-1;
  438.  
  439.                         subCode=winId[idObject[i]];
  440.  
  441.  
  442.                         try{
  443.                             window.df.setEntrySelected(-1,1,winId[idObject[i]]);
  444.                         }catch (Exception ex){
  445.                             window.messageWindow.setErrorMessage( ex );
  446.                         }
  447.                         String str2 = str0.substring(ind +1).trim();
  448.                         
  449.                         str1 = path + ":" + str1;
  450.                         if(subCode >=0){
  451.                             Substance substance = new Substance(subCode,str1,str2,window);
  452.                             //window.vSubstance.addElement(substance);
  453.                             substance.setVisible(true);
  454.                             substance.setLocation(150+i*20,100+i*10);
  455.                         }
  456.                     }
  457.  
  458.  
  459.                 }else if (parent == 2) {
  460.                     int length = object.length;
  461.  
  462.                     for(int i = 0; i < object.length  ; i++) {
  463.                         String str0 = (String) object[i];
  464.  
  465.                         int ind = str0.indexOf("]");
  466.                         String str1 = str0.substring(1,ind).trim();;
  467.  
  468.                         int rctCode =-1;
  469.                         String cname;
  470.                         rctCode= winId[idObject[i]];
  471.                         
  472.                         cname=className[idObject[i]];
  473.  
  474.                         try{
  475.                             window.df.setEntrySelected(-1,2,winId[idObject[i]]);
  476.                         }catch(Exception ex){
  477.                             window.messageWindow.setErrorMessage( ex );
  478.                         }
  479.                         String str2 = str0.substring(ind +1).trim();
  480.  
  481.                         if(rctCode>=0){
  482.                             Reactor reactor = new Reactor(rctCode,str1,str2,cname,window);
  483.                             //window.vReactor.addElement(reactor);
  484.                             reactor.setVisible(true);
  485.                             reactor.setLocation(150+i*20,100+i*10);
  486.                         }
  487.                     }
  488.                 }
  489.                 this.setVisible(false);
  490.                 this.dispose();
  491.  
  492.             }else {
  493.                 Object message ="No item selected ?";
  494.                 String title ="EntrySelector";
  495.                 int messageType = JOptionPane.INFORMATION_MESSAGE;
  496.                 JOptionPane.showMessageDialog(this.getContentPane(),message,
  497.                                                         title,messageType);
  498.             }
  499.         }
  500.         
  501.         if(e.getSource()== buttonCancel){
  502.             this.setVisible(false);
  503.         }
  504.     }
  505.  
  506.  
  507. }
  508.  
  509.  
  510.  
  511. /**
  512.  *MyTreeModel Class
  513.  * @author         XD.Zheng
  514.  * @version  1.0
  515.  * @since  JDK1.2.2
  516.  */
  517. class MyTreeModel
  518. {
  519.     /**
  520.      * Instance of EntrySelector 
  521.      */
  522.     EntrySelector entry;
  523.  
  524.     /**
  525.      * Constructor 
  526.      * @param es  Instance of EntrySelector
  527.      */
  528.     public MyTreeModel(EntrySelector es){
  529.         this.entry =es;
  530.     }
  531.  
  532.     /**
  533.      *EntrySelector  Read the tree-structure sentence.
  534.      *@param s      Tree-structure sentence
  535.      *@return TreeModel
  536.      */
  537.     public TreeModel getMyTreeModel2(String s){
  538.         Vector vModel = new Vector();
  539.         Vector vt= new Vector();
  540.         DefaultMutableTreeNode root=null;
  541.         DefaultMutableTreeNode child;
  542.         String currentNode="";
  543.  
  544.         int modeFlag =0;
  545.         int kai =0;
  546.         int oldkai=0;
  547.  
  548.         try {
  549.  
  550.  
  551.             StringReader in2 = new StringReader(s);
  552.             BufferedReader in3 = new BufferedReader(in2);
  553.  
  554.             while(true) {
  555.                 String str = in3.readLine();
  556.  
  557.                 if(str == null) break;
  558.  
  559.                 StringTokenizer st = new StringTokenizer(str,"\t");
  560.                 String line = st.nextToken();
  561.                 oldkai=kai;
  562.  
  563.                 kai= Integer.parseInt(line);
  564.                 int size = vt.size();
  565.                 if(kai!=65536 && kai!=65537){
  566.                     if((oldkai==65536 ||oldkai==65537) && vModel.size()!=0){
  567.                         entry.listmode.put(currentNode+oldkai,vModel.clone());
  568.                         
  569.                     }
  570.                     if(kai==0){
  571.                         root= new DefaultMutableTreeNode(st.nextToken());
  572.                         vt.addElement(root);
  573.                     }else{
  574.                         modeFlag =0;
  575.                         String nt = st.nextToken();
  576.                         currentNode =nt;
  577.                         child= new DefaultMutableTreeNode(nt);
  578.                         ((DefaultMutableTreeNode)vt.elementAt(kai-1)).add(child);
  579.                         if(size<=kai){
  580.                             vt.addElement(child);
  581.                         }else{
  582.                             vt.setElementAt(child,kai);
  583.                         }
  584.                     }
  585.                 }else{
  586.                     if(oldkai!=kai){
  587.                         if(vModel.size()!=0){
  588.                             entry.listmode.put(currentNode+oldkai,vModel.clone());
  589.                         }
  590.                         vModel = new Vector();
  591.                     }
  592.  
  593.                     String koumoku = st.nextToken()+"\t["+ st.nextToken() +"     ] "+ st.nextToken();
  594.                     if(st.hasMoreTokens()){
  595.                         koumoku += "\t" +st.nextToken();
  596.                     }else{
  597.                         koumoku += "\t" +"$";
  598.                     }
  599.                     vModel.addElement(koumoku);
  600.  
  601.  
  602.                 }
  603.  
  604.             }
  605.  
  606.             if(vModel.size()!=0){
  607.                 entry.listmode.put(currentNode+kai,vModel.clone());
  608.             }
  609.             vModel = new Vector();
  610.   
  611.             in3.close();
  612.         } catch(java.io.IOException e) {
  613.             entry.window.messageWindow.setErrorMessage( e );
  614.             
  615.         }
  616.         return new DefaultTreeModel(root);
  617.     }
  618.  
  619. }
  620.  
  621.  
  622.